package edu.spec.student.service.impl;

import edu.spec.student.dao.iface.ILectureDao;
import edu.spec.student.dao.iface.IStudentDao;
import edu.spec.student.dao.iface.IStudentLectureDao;
import edu.spec.student.model.lecture.Lecture;
import edu.spec.student.model.lecture.LectureDto;
import edu.spec.student.model.lecture.StudentLecture;
import edu.spec.student.service.iface.ILectureService;
import edu.spec.basic.model.enumeration.Status;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LectureService implements ILectureService {

    @Inject
    private ILectureDao lectureDao;
    @Inject
    private IStudentLectureDao studentLectureDao;
    @Inject
    private IStudentDao studentDao;

    @Override
    public void add(Lecture l) {
        lectureDao.add(l);
    }

    @Override
    public void delete(Integer id) {
        lectureDao.delete(id);
    }

    @Override
    public void update(Lecture l) {
        lectureDao.update(l);
    }

    @Override
    public Pager<Lecture> findByCon(Status status, String keyword) {
        return lectureDao.findByCon(status, keyword);
    }

    @Override
    public Map<String, List> listStudentLecture(Integer id) {
        List<Lecture> latestLecture = lectureDao.listLatestLecture();
        List<Integer> expectedLectureIds = studentLectureDao.listExpectedLectureIds(id);
        List<LectureDto> underwayLectureDto = studentLectureDao.listUnderwayLectureDto(id);
        List<Lecture> attendedLecture = studentLectureDao.listAttendedLecture(id);
        Map<String, List> eventMap = new HashMap();
        eventMap.put("latestLecture", latestLecture);
        eventMap.put("expectedLectureIds", expectedLectureIds);
        eventMap.put("underwayLectureDto", underwayLectureDto);
        eventMap.put("attendedLecture", attendedLecture);
        return eventMap;
    }

    @Override
    public Lecture load(Integer id) {
        return lectureDao.load(id);
    }

    @Override
    public void attend(Integer studentId, Integer lectureId) {
        StudentLecture sl = new StudentLecture();
        sl.setStudent(studentDao.load(studentId));
        sl.setLecture(lectureDao.load(lectureId));
        studentLectureDao.add(sl);
        return;
    }

    @Override
    public void toggleStatus(Integer id) {
        Lecture l = lectureDao.load(id);
        if(l.getStatus()==Status.DISABLED){
            l.setStatus(Status.ENABLED);
        }else if(l.getStatus()==Status.ENABLED){
            l.setStatus(Status.DISABLED);
        }
        lectureDao.update(l);
    }
}
